package com.blue.utils;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.InputStream;

public class SBTFxDialog {

	public enum Response {  
        NO, YES, CANCEL  
    };  
  
    private static Response buttonSelected = Response.CANCEL;  
  
    private static ImageView icon = new ImageView();  
  
    static class Dialog extends Stage {  
        public Dialog(String title, Stage owner, Scene scene) {  
            setTitle(title);  
            initStyle(StageStyle.UTILITY);  
            initModality(Modality.NONE);
            initOwner(owner);  
            setResizable(true);  
            setScene(scene);
            scene.getStylesheets().add(getClass().getResource("/css/Dialog.css").toExternalForm());
            InputStream imgInput=null;
            //File imgFile =new File( String.valueOf( Thread.currentThread().getContextClassLoader().getResource("image/ok.jpg") ) );
            /*File imgFile =new File(  getClass().getResource("/image/ok.jpg").toExternalForm()  );
            try {
    			imgInput = new FileInputStream(imgFile);
    		} catch (FileNotFoundException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            icon.setImage(new Image(imgInput));*/
        }  
        public void showDialog() {  
            sizeToScene();  
            centerOnScreen();  
            showAndWait();  
        }  
    }  
  
    static class Message extends Text {  
        public Message(String msg) {  
            super(msg);  
            setWrappingWidth(250);//自动换行的宽度  
        }  
    }  
  
    public static Response showConfirmDialog(Stage owner, String message,String title) {  
        VBox vb = new VBox();  
        Scene scene = new Scene(vb);  
        final Dialog dial = new Dialog(title, owner, scene);  
        vb.setPadding(new Insets(10, 10, 10, 10));  
        vb.setSpacing(10);  
        Button yesButton = new Button("确定");  
        yesButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                dial.close();  
                buttonSelected = Response.YES;  
            }  
        });  
        Button noButton = new Button("取消");  
        noButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                dial.close();  
                buttonSelected = Response.NO;  
            }  
        });  
        BorderPane bp = new BorderPane();  
        HBox buttons = new HBox();  
        buttons.setAlignment(Pos.CENTER);  
        buttons.setSpacing(10);  
        buttons.getChildren().addAll(yesButton, noButton);  
        bp.setCenter(buttons);  
        HBox msg = new HBox();  
        msg.setSpacing(5);  
        msg.getChildren().addAll(icon,new Message(message));  
        vb.getChildren().addAll(msg, bp);  
        dial.showDialog();  
        return buttonSelected;  
    }  
    public static void showMessageDialog(Stage owner, String message,String title) {  
        showMessageDialog(owner, new Message(message), title);  
    }
    
    public static void addMachinePanels(Stage owner,TableView<MachineInfo> tableview, String message, String title) { 
    	addMachinePanel(owner,tableview, new Message(message), title);
    }
    
    public static void addMachineGridPanel(Stage owner, String message, String title) { 
    	addMachineGridPanel(owner, new Message(message), title);
    }
    //用以识别html语句  
    public static void showWebViewDialog(Stage owner,String text_,String title){  
        VBox vb = new VBox();  
        Scene scene = new Scene(vb);  
        final Dialog dial = new Dialog(title, owner, scene);  
        vb.setPadding(new Insets(10, 10, 10, 10));  
        vb.setSpacing(10);  
        Button okButton = new Button("确定");  
        okButton.setAlignment(Pos.CENTER);  
        okButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                dial.close();  
            }  
        });  
        BorderPane bp = new BorderPane();  
        bp.setCenter(okButton);  
        HBox msg = new HBox();  
        msg.setSpacing(5);  
        WebView webview = new WebView();  
        WebEngine engine = webview.getEngine();  
        engine.loadContent(text_);  
        msg.getChildren().addAll(icon, webview);  
        vb.getChildren().addAll(msg, bp);  
        dial.setMinHeight(150);  
        dial.setMaxHeight(150);  
        dial.setHeight(150);  
        dial.showDialog();  
          
    }  
    public static void showMessageDialog(Stage owner, Message message, String title) {  
        VBox vb = new VBox();  
        Stage dialogStage = owner;
        Scene scene = new Scene(vb,Color.LIGHTBLUE);
        message.setFont(new Font(13));
        final Dialog dial = new Dialog(title, dialogStage, scene);  
        vb.setPadding(new Insets(30, 30, 30, 30));  
        vb.setSpacing(10);
        Button okButton = new Button("返回");  
        okButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                dial.close();  
            }  
        });  
        BorderPane bp = new BorderPane();
        bp.setCenter(okButton);  
        HBox msg = new HBox();  
        msg.setSpacing(25);  
        VBox textbox = new VBox();
        textbox.setPadding(new Insets(15,0,0,0));
        textbox.getChildren().add(message);
        msg.getChildren().addAll(icon,textbox);
        vb.getChildren().addAll(msg, bp);
        dial.showDialog();
    }
    
    public static void addMachineGridPanel(Stage owner, Node message, String title) {
    	
    	VBox newShowPanel = new VBox();
        
    	GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));
        
        Label userName = new Label("User Name:");
        grid.add(userName, 0, 1);

        final TextField userTextField = new TextField();
        grid.add(userTextField, 1, 1);

        Label pw = new Label("Password:");
        grid.add(pw, 0, 2);

        final PasswordField pwBox = new PasswordField();
        grid.add(pwBox, 1, 2);
        
        
    	Scene scene = new Scene(newShowPanel);  
        final Dialog dial = new Dialog(title, owner, scene);  
        
        
    	HBox buttonHBox = new HBox(5);
    	//buttonHBox.setPadding(new Insets(5, 82, 5, 12));
        Button okButton = new Button("确定");  
        okButton.setAlignment(Pos.CENTER);  
        okButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                //dial.close();  
            }  
        }); 
        
        Button cancelButton = new Button("取消");  
        cancelButton.setAlignment(Pos.CENTER);  
        cancelButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
                dial.close();  
            }  
        });  
        buttonHBox.getChildren().addAll(okButton,cancelButton);
        
        grid.add(buttonHBox, 1, 4);
        
        
        
        BorderPane bp = new BorderPane();  
        bp.setCenter(buttonHBox);  
        HBox msg = new HBox();  
        msg.setSpacing(5);  
        msg.getChildren().addAll(icon, message); 
        
        //分割线  
        Separator sper1 = new Separator();  
        sper1.setOrientation(Orientation.HORIZONTAL);  
        
        newShowPanel.getChildren().addAll(msg,sper1,grid);
        
        dial.showDialog();
    	
    }
    
    // *** javaFX 可控制自动关闭时间的弹出提示窗口 
    /*public static void showTimedDialog(long time, String message) {  
        Stage popup = new Stage();

        //popup.setAlwaysOnTop(true);  
        popup.initModality(Modality.APPLICATION_MODAL);  
        Button closeBtn = new Button("知道了");  
        closeBtn.setOnAction(e -> {  
            popup.close();  
        });  
        VBox root = new VBox();  
        root.setPadding(new Insets(20));  
        root.setAlignment(Pos.BASELINE_CENTER);  
        root.setSpacing(20);  
        root.getChildren().addAll(new Label(message), closeBtn);  
        Scene scene = new Scene(root);  
        popup.setScene(scene);  
        popup.setTitle("提示信息");  
        popup.show();  
      
        Thread thread = new Thread(() -> {  
            try {  
                Thread.sleep(time);  
                if (popup.isShowing()) {  
                    Platform.runLater(() -> popup.close());  
                }  
            } catch (Exception exp) {  
                exp.printStackTrace();  
            }  
        });  
        thread.setDaemon(true);  
        thread.start();  
    }  */
    
    public static void addMachinePanel(Stage owner,final TableView<MachineInfo> tableview, Node message, String title) { 
    	
    	TextField userTextFieldInfo = new TextField();
    	final TextField tf123= new TextField();
    	final TextField portTextField= new TextField();
    	VBox newShowPanel = new VBox();
    	Scene scene = new Scene(newShowPanel);  
        final Dialog dial = new Dialog(title, owner, scene);  
        newShowPanel.setPadding(new Insets(10, 10, 10, 10));  
        newShowPanel.setSpacing(10); 
        
        
    	HBox buttonHBox = new HBox(10);
    	buttonHBox.setPadding(new Insets(5, 102, 5, 12));
        Button okButton = new Button("确定");  
        okButton.setAlignment(Pos.CENTER);  
        okButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
            	MachineInfo machineInfo = new MachineInfo();
            	String ipAddress="",portInfo="";
            	if ( tf123.getText() != null && !tf123.getText().isEmpty()) {
            		
            		ipAddress= tf123.getText();
            		machineInfo.setIp_address(ipAddress);
            		machineInfo.setMachine_name(ipAddress);
            		
            		machineInfo.setUser_name("test");
            		System.out.println("ipAddress ::"+ipAddress);
            	}
            	if ( portTextField.getText() != null && !portTextField.getText().isEmpty()) {
            		portInfo = portTextField.getText();
            		machineInfo.setPass_word(portInfo);
            		
            	}
            	machineInfo.setOs_name("Aix");
            	machineInfo.setMachine_id("131");
            	machineInfo.setPass_word("123jjjj");
            	machineInfo.setFlag("144");
            	//MySqlMethod mySqlMethod = new MySqlMethod();
            	//mySqlMethod.insertMachineInfo(machineInfo);
                //dial.close();  
            	
            	
            	//刷新数据列表
            	tableview.getItems().clear();
            	ObservableList<MachineInfo> relist = FXCollections.observableArrayList();
            	//relist = mySqlMethod.getMysqlList();
            	tableview.setItems(relist);
            	
            }  
        }); 
        
        Button cancelButton = new Button("取消");  
        cancelButton.setAlignment(Pos.CENTER);  
        cancelButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {
                dial.close();  
            }  
        });  
        //buttonHBox.getChildren().add(5, okButton);
        //buttonHBox.getChildren().add(6, cancelButton);
        buttonHBox.getChildren().addAll(okButton,cancelButton);
        
        BorderPane bp = new BorderPane();  
        bp.setCenter(buttonHBox);  
        
        HBox msg = new HBox();  
        msg.setSpacing(5);  
        msg.getChildren().addAll(icon, message);  
        //newShowPanel.getChildren().addAll(msg, bp);
        
    	
        
    	//Label titleLabel = new Label("支付密码器管理 >>Tomcat安装设置");
    	VBox tophb = new VBox(5);
    	tophb.setPadding(new Insets(0, 12, 0, 12));  
        //Label labelct123 = new Label("请输入");  //外面标题  
        //labelct123.setFont(new Font(15));  
        HBox center_hb123 = new HBox(5);
        center_hb123.setPadding(new Insets(3, 12, 3, 0));
        Label label123 = new Label("机器IP：");

          
        tf123.setPrefColumnCount(15);  //设置偏爱的文本框宽度  
        tf123.setPromptText("请输入主机的IP地址");
        tf123.setText(" 999999");
        //Button btn11query = new Button("查询");  
        //btn11query.setPrefSize(60, 20);  
        center_hb123.getChildren().addAll(label123,tf123);  
        tophb.getChildren().addAll(center_hb123); 
    	
    	//中间一行  
        VBox centervb = new VBox(5);  
        centervb.setPadding(new Insets(0, 12, 0, 12));  
        //Label labelct = new Label("请设置Tomcat的启动端口，该端口用户Tomcat服务启动时Http服务启动的端口；");  //外面标题  
        //labelct.setFont(new Font(15));  
        HBox center_hb = new HBox(5);  
        center_hb.setPadding(new Insets(3, 12, 3, 0));  
        Label label1 = new Label("端口编号：");  

          
        portTextField.setPrefColumnCount(10);  //设置偏爱的文本框宽度  
        portTextField.setPromptText("请输入主机的端口");
        //Button btnquery = new Button("查询");  
        //btnquery.setPrefSize(60, 20);  
        center_hb.getChildren().addAll(label1,portTextField);  
        centervb.getChildren().addAll(center_hb);
        
        HBox userHbox = new HBox(5);
        userHbox.setPadding(new Insets(3, 12, 3, 12));
        Label userNameLabel = new Label("用户名称：");  
        
        
        userTextFieldInfo.setPrefColumnCount(15);
        userTextFieldInfo.setPromptText("请输入用户名称");
        userHbox.getChildren().addAll(userNameLabel,userTextFieldInfo);
        
        
        //分割线  
        Separator sper1 = new Separator();  
        sper1.setOrientation(Orientation.HORIZONTAL);  
        Separator sper2 = new Separator();  
        sper2.setOrientation(Orientation.HORIZONTAL);
        Separator sper3 = new Separator();  
        sper3.setOrientation(Orientation.HORIZONTAL);
        Separator sper4 = new Separator();  
        sper4.setOrientation(Orientation.HORIZONTAL);
        
        //newShowPanel.getChildren().addAll(msg, bp);
        newShowPanel.getChildren().addAll(msg,sper1,tophb,sper2,centervb,sper3,userHbox,sper4,buttonHBox);
        
          
        dial.showDialog();  
    }
    
    public static void updateMachinePanel(Stage owner,final TableView<MachineInfo> tableview,MachineInfo userTest, String message, String title) { 
    	
    	TextField userTextFieldInfo = new TextField();
    	final TextField tf123= new TextField();
    	final TextField portTextField= new TextField();
    	VBox newShowPanel = new VBox();
    	Scene scene = new Scene(newShowPanel);  
        final Dialog dial = new Dialog(title, owner, scene);  
        newShowPanel.setPadding(new Insets(10, 10, 10, 10));  
        newShowPanel.setSpacing(10); 
        
        
    	HBox buttonHBox = new HBox(10);
    	buttonHBox.setPadding(new Insets(5, 102, 5, 12));
        Button okButton = new Button("确定");  
        okButton.setAlignment(Pos.CENTER);  
        okButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {  
            	MachineInfo machineInfo = new MachineInfo();
            	String ipAddress="",portInfo="";
            	if ( tf123.getText() != null && !tf123.getText().isEmpty()) {
            		
            		ipAddress= tf123.getText();
            		machineInfo.setIp_address(ipAddress);
            		machineInfo.setMachine_name(ipAddress);
            		
            		machineInfo.setUser_name("test");
            		System.out.println("ipAddress ::"+ipAddress);
            	}
            	if ( portTextField.getText() != null && !portTextField.getText().isEmpty()) {
            		portInfo = portTextField.getText();
            		machineInfo.setPass_word(portInfo);
            		
            	}
            	machineInfo.setOs_name("Aix");
            	machineInfo.setMachine_id("131");
            	machineInfo.setPass_word("123jjjj");
            	machineInfo.setFlag("144");
            	//MySqlMethod mySqlMethod = new MySqlMethod();
            	//mySqlMethod.insertMachineInfo(machineInfo);
                //dial.close();  
            	
            	
            	//刷新数据列表
            	tableview.getItems().clear();
            	ObservableList<MachineInfo> relist = FXCollections.observableArrayList();
            	//relist = mySqlMethod.getMysqlList();
            	tableview.setItems(relist);
            	
            }  
        }); 
        
        Button cancelButton = new Button("取消");  
        cancelButton.setAlignment(Pos.CENTER);  
        cancelButton.setOnAction(new EventHandler<ActionEvent>() {  
            @Override  
            public void handle(ActionEvent e) {
                dial.close();  
            }  
        });  
        //buttonHBox.getChildren().add(5, okButton);
        //buttonHBox.getChildren().add(6, cancelButton);
        buttonHBox.getChildren().addAll(okButton,cancelButton);
        
        BorderPane bp = new BorderPane();  
        bp.setCenter(buttonHBox);  
        
        HBox msg = new HBox();  
        msg.setSpacing(5);  
        msg.getChildren().addAll(icon, new Message(message));  
        //newShowPanel.getChildren().addAll(msg, bp);
        
    	
        
    	//Label titleLabel = new Label("支付密码器管理 >>Tomcat安装设置");
    	VBox tophb = new VBox(5);
    	tophb.setPadding(new Insets(0, 12, 0, 12));  
        //Label labelct123 = new Label("请输入");  //外面标题  
        //labelct123.setFont(new Font(15));  
        HBox center_hb123 = new HBox(5);
        center_hb123.setPadding(new Insets(3, 12, 3, 0));
        Label label123 = new Label("机器IP：");

          
        tf123.setPrefColumnCount(15);  //设置偏爱的文本框宽度  
        tf123.setPromptText("请输入主机的IP地址");
        tf123.setText(userTest.getIp_address());
        //Button btn11query = new Button("查询");  
        //btn11query.setPrefSize(60, 20);  
        center_hb123.getChildren().addAll(label123,tf123);  
        tophb.getChildren().addAll(center_hb123); 
    	
    	//中间一行  
        VBox centervb = new VBox(5);  
        centervb.setPadding(new Insets(0, 12, 0, 12));  
        //Label labelct = new Label("请设置Tomcat的启动端口，该端口用户Tomcat服务启动时Http服务启动的端口；");  //外面标题  
        //labelct.setFont(new Font(15));  
        HBox center_hb = new HBox(5);  
        center_hb.setPadding(new Insets(3, 12, 3, 0));  
        Label label1 = new Label("端口编号：");  

          
        portTextField.setPrefColumnCount(10);  //设置偏爱的文本框宽度  
        portTextField.setPromptText("请输入主机的端口");
        //Button btnquery = new Button("查询");  
        //btnquery.setPrefSize(60, 20);  
        center_hb.getChildren().addAll(label1,portTextField);  
        centervb.getChildren().addAll(center_hb);
        
        HBox userHbox = new HBox(5);
        userHbox.setPadding(new Insets(3, 12, 3, 12));
        Label userNameLabel = new Label("用户名称：");  
        
        
        userTextFieldInfo.setPrefColumnCount(15);
        userTextFieldInfo.setPromptText("请输入用户名称");
        userHbox.getChildren().addAll(userNameLabel,userTextFieldInfo);
        
        
        //分割线  
        Separator sper1 = new Separator();  
        sper1.setOrientation(Orientation.HORIZONTAL);  
        Separator sper2 = new Separator();  
        sper2.setOrientation(Orientation.HORIZONTAL);
        Separator sper3 = new Separator();  
        sper3.setOrientation(Orientation.HORIZONTAL);
        Separator sper4 = new Separator();  
        sper4.setOrientation(Orientation.HORIZONTAL);
        
        //newShowPanel.getChildren().addAll(msg, bp);
        newShowPanel.getChildren().addAll(msg,sper1,tophb,sper2,centervb,sper3,userHbox,sper4,buttonHBox);
        
          
        dial.showDialog();  
    }
}
